//
//  UIView+NetResultView.m
//  AirTraffic
//
//  Created by yby on 2019/9/19.
//  Copyright © 2019 天九网络科技. All rights reserved.
//

#import "UIView+NetResultView.h"

@interface UIView ()

@property (nonatomic,copy) void (^reloadAction)(void);
@property (nonatomic,copy) void (^backAction)(void);
@property (nonatomic,copy) void (^pushAction)(void);
@end

@implementation UIView (NetResultView)
- (void)setReloadAction:(void (^)(void))reloadAction {
    objc_setAssociatedObject(self, @selector(reloadAction), reloadAction, OBJC_ASSOCIATION_COPY);
}
- (void (^)(void))reloadAction {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setBackAction:(void (^)(void))backAction {
    objc_setAssociatedObject(self, @selector(backAction), backAction, OBJC_ASSOCIATION_COPY);
}
- (void (^)(void))backAction {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)setPushAction:(void (^)(void))pushAction {
    objc_setAssociatedObject(self, @selector(pushAction), pushAction, OBJC_ASSOCIATION_COPY);
}
- (void (^)(void))pushAction {
    return objc_getAssociatedObject(self, _cmd);
}

#pragma mark - 配置按钮
- (void)wb_configReloadAction:(void (^)(void))block {
    self.reloadAction = block;
    if (self.var_emptyControlView && self.reloadAction) {
        self.var_emptyControlView.didClickReloadBlock = self.reloadAction;
    }
}
- (void)wb_configBackAction:(void (^)(void))block {
    self.backAction = block;
    if (self.var_emptyControlView && self.backAction) {
        self.var_emptyControlView.didClickBackBlock = self.backAction;
    }
}
- (void)wb_configPushAction:(void (^)(void))block {
    self.pushAction = block;
    if (self.var_emptyControlView && self.pushAction) {
        self.var_emptyControlView.didClickPushBlock = self.pushAction;
    }
}
- (void)wb_configControlButtonColor:(UIColor *)color {
    if (self.var_emptyControlView) {
        [self.var_emptyControlView.var_controlButton setTitleColor:color forState:UIControlStateNormal];
        self.var_emptyControlView.var_controlButton.layer.borderColor = color.CGColor;
    }
}
- (void)wb_configControlButtonTitle:(NSString *)title {
    if (self.var_emptyControlView) {
        [self.var_emptyControlView.var_controlButton setTitle:title forState:UIControlStateNormal];
        
        UILabel *var_controlLabel = [[UILabel alloc] init];
        var_controlLabel.text = title;
        var_controlLabel.numberOfLines = 1;
        var_controlLabel.font = [UIFont systemFontOfSize:16];
        CGFloat var_controlLabelH = [var_controlLabel sizeThatFits:CGSizeMake(kScreenWidth-Ratio(60), MAXFLOAT)].height;
        CGFloat var_controlLabelW = [var_controlLabel sizeThatFits:CGSizeMake(MAXFLOAT, var_controlLabelH)].width;
        [self.var_emptyControlView.var_controlButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.mas_equalTo(self.var_emptyControlView.var_tipLabel.mas_bottom).offset(30);
            make.width.mas_equalTo(var_controlLabelW + 40);
            make.height.mas_equalTo(var_controlLabelH + 20);
        }];
    }
}

- (UIViewController *)wb_viewController {
    //获取当前view的superView对应的控制器
    UIResponder *var_next = [self nextResponder];
    do {
        if ([var_next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)var_next;
        }
        var_next = [var_next nextResponder];
    } while (var_next != nil);
    return nil;
}


#pragma mark ===== TJEmptyControlView 方法
- (void)setVar_emptyControlView:(HTEmptyControlView *)emptyControlView {
    [self willChangeValueForKey:NSStringFromSelector(@selector(var_emptyControlView))];
    objc_setAssociatedObject(self, @selector(var_emptyControlView), emptyControlView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:NSStringFromSelector(@selector(var_emptyControlView))];
}
- (HTEmptyControlView *)var_emptyControlView {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)wb_showEmptyControlViewWitnFrame:(CGRect)frame
                           fullScreen:(BOOL)isFull
                            imageType:(HTEmptyImageType)imageType
                            titleName:(NSString *)titleName
                           buttonType:(HTEmptyButtonType)buttonType
                           buttonName:(NSString *)buttonName
{
    
    if (self.var_emptyControlView.superview) {
        [self.var_emptyControlView removeFromSuperview];
    }
    
    if(!self.var_emptyControlView && isFull) {
        self.var_emptyControlView = [[HTEmptyControlView alloc] initWithFrame:self.bounds buttonType:buttonType];
    }
    
    switch (imageType) {
        case HTEmptyImageTypeNetBad: {
            self.var_emptyControlView.hidden = NO;
            self.var_emptyControlView.var_imageView.image = [UIImage imageNamed:@"common_no_data"];
            self.var_emptyControlView.var_tipLabel.text = titleName;
        }
            break;
        case HTEmptyImageTypeNoData: {
            self.var_emptyControlView.hidden = NO;
            self.var_emptyControlView.var_imageView.image = [UIImage imageNamed:@"common_no_data"];
            self.var_emptyControlView.var_tipLabel.text = titleName;
        }
            break;
        default:
            break;
    }
    switch (buttonType) {
        case HTEmptyButtonTypeHidden: {
            self.var_emptyControlView.var_controlButton.hidden = YES;
        }
            break;
        case HTEmptyButtonTypeBack:
        case HTEmptyButtonTypeReload:
        case HTEmptyButtonTypePush: {
            self.var_emptyControlView.var_controlButton.hidden = NO;
            [self.var_emptyControlView.var_controlButton setTitle:buttonName forState:UIControlStateNormal];
        }
            break;
        default:
            [self.var_emptyControlView.var_controlButton setTitle:[NSString stringWithFormat:@"%@",buttonName] forState:UIControlStateNormal];
            break;
    }
    [self addSubview:self.var_emptyControlView];
    [self bringSubviewToFront:self.var_emptyControlView];
}
- (void)wb_showEmptyControlViewWitnFrame:(CGRect)frame
                           fullScreen:(BOOL)isFull
                            imageName:(NSString *)imageName
                            titleName:(NSString *)titleName
                           buttonType:(HTEmptyButtonType)buttonType
                           buttonName:(NSString *)buttonName
{
    if (self.var_emptyControlView.superview) { [self.var_emptyControlView removeFromSuperview]; }
    
    if(!self.var_emptyControlView && isFull) {
        self.var_emptyControlView = [[HTEmptyControlView alloc] initWithFrame:self.bounds buttonType:buttonType];
    }
    if (isEmpty(imageName)) {
        self.var_emptyControlView.hidden = NO;
        self.var_emptyControlView.var_imageView.hidden = YES;
    }else {
        self.var_emptyControlView.hidden = NO;
        self.var_emptyControlView.var_imageView.image = [UIImage imageNamed:imageName];
    }
    self.var_emptyControlView.var_tipLabel.text = isEmpty(titleName) ? @"" : titleName;
    switch (buttonType) {
        case HTEmptyButtonTypeHidden: {
            self.var_emptyControlView.var_controlButton.hidden = YES;
        }
            break;
        case HTEmptyButtonTypeBack:
        case HTEmptyButtonTypeReload:
        case HTEmptyButtonTypePush: {
            self.var_emptyControlView.var_controlButton.hidden = NO;
            [self.var_emptyControlView.var_controlButton setTitle:buttonName forState:UIControlStateNormal];
        }
            break;
        default:
            break;
    }
    [self addSubview:self.var_emptyControlView];
    [self bringSubviewToFront:self.var_emptyControlView];
}
- (void)wb_hiddenEmptyControlView {
    if (self.var_emptyControlView) {
        [self.var_emptyControlView removeFromSuperview];
    }
}



#pragma mark ===== TJLoadingPageView 方法
- (void)setWb_loadingView:(HTLoadingPageView *)loadingView {
    [self willChangeValueForKey:NSStringFromSelector(@selector(wb_loadingView))];
    objc_setAssociatedObject(self, @selector(wb_loadingView), loadingView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self didChangeValueForKey:NSStringFromSelector(@selector(wb_loadingView))];
}
- (HTLoadingPageView *)wb_loadingView {
    return objc_getAssociatedObject(self, _cmd);
}
- (void)wb_beginLoading {
    if (self.var_emptyControlView.superview) { [self.var_emptyControlView removeFromSuperview]; }
    
    if (!self.var_loadingView) {
        self.var_loadingView = [[HTLoadingPageView alloc] initWithFrame:self.bounds];
    }
    [self addSubview:self.var_loadingView];
    [self bringSubviewToFront:self.var_loadingView];
    
    [self.var_loadingView wb_startAnimation];
}
- (void)wb_endLoading {
    if (self.var_loadingView) {
        [self.var_loadingView wb_stopAnimation];
        [self.var_loadingView removeFromSuperview];
        self.var_loadingView = nil;
    }
}
@end



#pragma mark ===== TJEmptyControlView类
@interface HTEmptyControlView ()
@end
@implementation HTEmptyControlView
- (instancetype)init {
    return [self initWithFrame:CGRectZero];
}
- (void)dealloc {
    NSLog(@"dealloc-%s",__func__);
}
- (instancetype)initWithFrame:(CGRect)frame buttonType:(HTEmptyButtonType)buttonType {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        UIImageView* var_imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"blank_content"]];
        _var_imageView = var_imageView;
        [self addSubview:_var_imageView];
        
        UILabel* var_tipLabel = [[UILabel alloc]init];
        var_tipLabel.numberOfLines = 2;
        var_tipLabel.font = [UIFont boldSystemFontOfSize:14];
        var_tipLabel.textAlignment = NSTextAlignmentCenter;
        var_tipLabel.textColor = [UIColor colorWithHexString:@"#C0AED8"];
        _var_tipLabel = var_tipLabel;
        [self addSubview:_var_tipLabel];
        
        UIButton *var_controlButton = [UIButton buttonWithType:UIButtonTypeCustom];
        var_controlButton.backgroundColor = [UIColor colorFFE300];
        [var_controlButton setTitleColor:[UIColor color111] forState:UIControlStateNormal];
        [var_controlButton.titleLabel setFont:[UIFont systemFontOfSize:16 weight:UIFontWeightBold]];
        var_controlButton.layer.cornerRadius = 6;
        var_controlButton.layer.masksToBounds = YES;
//        controlButton.layer.borderColor = [UIColor colorWithRed:146/255.0f green:166/255.0f blue:198/255.0f alpha:1].CGColor;
//        controlButton.layer.borderWidth = 0.5;
        if (buttonType == HTEmptyButtonTypeBack) {
            [var_controlButton addTarget:self action:@selector(_clickControlBackButton:) forControlEvents:UIControlEventTouchUpInside];
        } else if (buttonType == HTEmptyButtonTypeReload) {
            [var_controlButton addTarget:self action:@selector(_clickControlReloadButton:) forControlEvents:UIControlEventTouchUpInside];
        } else if (buttonType == HTEmptyButtonTypePush) {
            [var_controlButton addTarget:self action:@selector(_clickControlPushButton:) forControlEvents:UIControlEventTouchUpInside];
        } else if (buttonType >1000)
        {
            [var_controlButton addTarget:self action:@selector(_clickControlReloadButton:) forControlEvents:UIControlEventTouchUpInside];
        }
        _var_controlButton = var_controlButton;
        [self addSubview:_var_controlButton];
        
        
        [_var_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Ratio(62));
            make.right.mas_equalTo(Ratio(-62));
//            make.centerY.mas_equalTo(self);
            if (buttonType == HTEmptyButtonTypeHidden) {
                make.centerY.mas_equalTo(self).offset(-50);
            } else {
                make.centerY.mas_equalTo(self).offset(50);;
            }
        }];
        [_var_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.var_tipLabel);
//            make.bottom.mas_equalTo(self.tipLabel.mas_top).offset(-20);
        }];
        [_var_controlButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.var_tipLabel.mas_bottom).offset(30);
            make.left.mas_equalTo(Ratio(15));
            make.right.mas_equalTo(Ratio(-15));
            make.height.mas_equalTo(Ratio(44));
//            make.centerX.equalTo(self);
//            make.top.mas_equalTo(self.tipLabel.mas_bottom).offset(30);
//            make.width.mas_equalTo(130);
//            make.height.mas_equalTo(40);
        }];
    }
    return self;
}
#pragma mark - public Method
- (void)_clickControlReloadButton:(id)sender {
    if (self.didClickReloadBlock) {
        self.didClickReloadBlock();
    }
}
- (void)_clickControlBackButton:(id)sender {
    if (self.didClickBackBlock) {
        self.didClickBackBlock();
    }
}
- (void)_clickControlPushButton:(id)sender {
    if (self.didClickPushBlock) {
        self.didClickPushBlock();
    }
}
@end


#pragma mark ===== TJLoadingPageView类
@interface HTLoadingPageView()
@property (nonatomic,weak) UIActivityIndicatorView * var_activityIndicatorView;
@property (nonatomic,weak) UILabel * var_loadingTipLabel;
@property (nonatomic,assign,readwrite) BOOL var_isLoading;
@end
@implementation HTLoadingPageView
- (instancetype)init {
    return [self initWithFrame:CGRectZero];
}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _var_isLoading = NO;
        self.backgroundColor = [UIColor whiteColor];
        UIActivityIndicatorView* var_activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        _var_activityIndicatorView = var_activityIndicatorView;
        [self addSubview:_var_activityIndicatorView];
        
        UILabel* var_loadingTipLabel = [[UILabel alloc]init];
        var_loadingTipLabel.numberOfLines = 1;
        var_loadingTipLabel.font = [UIFont systemFontOfSize:15];
        var_loadingTipLabel.textAlignment = NSTextAlignmentCenter;
        var_loadingTipLabel.textColor = [UIColor grayColor];
        var_loadingTipLabel.text =  @"加载中";
        _var_loadingTipLabel = var_loadingTipLabel;
        [self addSubview:_var_loadingTipLabel];
        
        [_var_activityIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.height.equalTo(@25);
            make.bottom.equalTo(self.mas_centerY).offset(-5);
        }];
        [_var_loadingTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self);
            make.height.equalTo(@50);
            make.top.equalTo(self.mas_centerY).offset(5);
        }];
        [self setHidden:NO];
    }
    return self;
}
#pragma mark - public Method
- (void)wb_startAnimation{
    self.hidden = NO;
    [_var_activityIndicatorView startAnimating];
    _var_isLoading = YES;
}
- (void)wb_stopAnimation{
    self.hidden = YES;
    [_var_activityIndicatorView stopAnimating];
    _var_isLoading = NO;
}

@end
